using UnityEngine;
using System.Collections;

public class Carryable : MonoBehaviour {

    private bool grounded;  // if grounded when player is extending, player can grab. If grounded when player is in grab or retract, then he's letting go.
    public float interactionTime;
    private float interactionTimer;

    public bool Grounded
    {
        get { return grounded; }
    }

	// Use this for initialization
	void Start () {

        // hack for first time pickup
        transform.position = new Vector3(transform.position.x, 0.45f, transform.position.z);
        grounded = true;

        if (interactionTime == 0)
            interactionTime = 0.2f;
    }
	
	// Update is called once per frame
	void Update () {

        if (interactionTimer - Time.deltaTime <= 0.0f)
            interactionTimer = 0.0f;
        else
            interactionTimer -= Time.deltaTime;

	}

    public bool cooldown()
    {
        return (interactionTime <= 0.0f);
    }

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Carryable collide " + other.transform.name);// + "." + other.transform.parent.name);
        
        // If we collided with a drop shadow, bleh.
        if (other.gameObject.GetComponent<DropShadow>() != null)
            return;

        // If the player picked us up, unground us
        if (other.transform.parent != null && other.transform.parent.tag == "Player")
        {
            Debug.Log("Carryable ungrounded");
            grounded = false;

            interactionTimer = interactionTime;

            // snap to the center of the player
            Vector3 vTemp = other.bounds.center;
            transform.position = new Vector3(vTemp.x, vTemp.y - other.bounds.size.y, vTemp.z);

            return;
        }

        // If we collided with the floor, become grounded to signal the player to LEMME GO
        else if (other.transform.tag == "Floor")
        {
            Debug.Log("Carryable grounded");
            grounded = true;

            interactionTimer = interactionTime;
        }
    }
}
